import { routerRedux } from "dva/router"
import { getPreData, getCommonList, getExportUser, getUserData, getOderData } from '../services/example'
import { Message } from 'antd'
export default {

  namespace: 'example',

  state: {
    dataList: [],
    commonList: [],
    userList: [],
    total: 0,
    page: 1,
    oderList:[],
    oderTotal:0,
  },

  subscriptions: {
   /**
     * 监听浏览器地址，当跳转到 /user 时进入该方法
     * @param dispatch 触发器，用于触发 effects 中的 query 方法
     * @param history 浏览器历史记录，主要用到它的 location 属性以获取地址栏地址
     */
    setup ({ dispatch, history }) {
      history.listen((location) => {
        // console.log('location is: %o', location);
        // console.log('重定向接收参数：%o', location.state)
        // 调用 effects 属性中的 query 方法，并将 location.state 作为参数传递 
        dispatch({
          type: 'query',
          payload: location.state,
        })
      });
    },
  },

  effects: {
    *fetch({ payload }, { call, put }) {  // eslint-disable-line
      yield put({ type: 'save' });
    },
    *redirect ({ payload }, { put }) {
      yield put(routerRedux.push('/oderData'));
    },
    *getPreDataList({ payload},{ call, put }){
      const data = yield call(getPreData,payload);
      if(!data.success){
        Message.error(data.msg)
        return
      }
      yield put({
        type: 'save',
        payload: {
          dataList: data.obj
        }
      })
    },
    *getCommonListData({ payload},{ call, put }){
      const data = yield call(getCommonList,payload);
      if(!data.success){
        Message.error(data.msg)
        return
      }
      yield put({
        type: 'save',
        payload: {
          commonList: data.obj
        }
      })
    },
    *getExportUserData({ payload},{ call, put }){
      const data = yield call(getExportUser,payload);
      console.log(data)
      if(data && !data.success){
        Message.error(data.msg)
        return
      }
      yield put({
        type: 'save',
        payload: {
          // commonList: data.obj
        }
      })
    },
    *getUserDataList({ payload},{ call, put }){
      const data = yield call(getUserData,payload);
      console.log(data)
      if(data && !data.success){
        Message.error(data.msg)
        return
      }
      yield put({
        type: 'save',
        payload: {
          userList: data.obj,
          total: data.totalCount,
          // page: data.page
        }
      })
    },
    *getOderDataList({ payload},{ call, put }){
      const data = yield call(getOderData,payload);
      console.log(data)
      if(data && !data.success){
        Message.error(data.msg)
        return
      }
      yield put({
        type: 'save',
        payload: {
          oderList: data.obj,
          oderTotal: data.totalCount,
          // page: data.page
        }
      })
    }
    

   
  },

  reducers: {
    save(state, action) {
      return { ...state, ...action.payload };
    },
    
  },

};
